package com.milorad.ads.data.dao.jpa;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceException;
import javax.persistence.TypedQuery;

import org.springframework.orm.jpa.JpaCallback;
import org.springframework.transaction.annotation.Transactional;

import com.milorad.ads.data.dao.interfaces.AdvertDao;
import com.milorad.ads.data.domain.Advert;

public class AdvertDaoImpl extends AbstractDaoJPAImpl<Advert> implements AdvertDao {

	public AdvertDaoImpl() {
		super(Advert.class);
	}
	
	@Transactional
	public List<Advert> findAll() {
		return getJpaTemplate().execute(new JpaCallback<List<Advert>>() {
			public List<Advert> doInJpa(EntityManager em) throws PersistenceException {
				TypedQuery<Advert> query = em.createQuery("select a from Advert a", Advert.class);
				return query.getResultList();
			}
		});
	}

	@Transactional
	public int countAll() {
		return getJpaTemplate().execute(new JpaCallback<Integer>() {

			public Integer doInJpa(EntityManager em) throws PersistenceException {
				TypedQuery<Long> query = em.createQuery("select count (a) from Advert a", Long.class);
				return (query.getSingleResult()).intValue();
			}
		});
	}

	@Transactional
	public List<Advert> find(final Advert advert) {
		
		return getJpaTemplate().execute(new JpaCallback<List<Advert>>() {
			public List<Advert> doInJpa(EntityManager em) throws PersistenceException {
				
				
				StringBuilder strQuery = new StringBuilder();
				strQuery.append("select a from Advert a");
				
				boolean previousConditionExists = false;
				
				if (advert.getCategory() != null || advert.getText() != null 
						|| advert.getStatus() != null || advert.getCity() != null) {
					strQuery.append(" where ");
				}
				
				if (advert.getCategory() != null) {
					strQuery.append(" a.category=:filterCategory ");					
					previousConditionExists = true;
				}
												
				if (advert.getText() != null) {
					if (previousConditionExists) {
						strQuery.append(" and ");
					}
					strQuery.append(" UPPER(a.text) LIKE :filterKeyWord ");
					previousConditionExists = true;
				}
				
				if (advert.getStatus() != null) {
					if (previousConditionExists) {
						strQuery.append(" and ");
					}
					strQuery.append(" a.status=:filterStatus ");
					previousConditionExists = true;
				}
				
				if (advert.getCity() != null) {
					if (previousConditionExists) {
						strQuery.append(" and ");
					}
					strQuery.append(" city=:filterCity ");
					previousConditionExists = true;
				}
				
				strQuery.append(" order by a.date desc");
								
				TypedQuery<Advert> query = em.createQuery(strQuery.toString(), Advert.class);
				
				if (advert.getCategory() != null) {
					query.setParameter("filterCategory", advert.getCategory());
				} 
				if (advert.getText() != null) {
					query.setParameter("filterKeyWord", "%" + advert.getText().toUpperCase() + "%");
				}
				if (advert.getStatus() != null) {
					query.setParameter("filterStatus", advert.getStatus());
				}
				if (advert.getCity() != null) {
					query.setParameter("filterCity", advert.getCity());
				}
				
				List<Advert> resp = query.getResultList();		
				
				return resp;
			}
		});
	}
	
}